package com.lczyfz.istep.modules.openstack.api.nova.impl.parseJson;

import com.lczyfz.istep.common.constants.SceneConstants;
import com.lczyfz.istep.modules.openstack.api.neutron.model.Network;
import com.lczyfz.istep.modules.openstack.api.nova.model.Flavor;
import com.lczyfz.istep.modules.openstack.api.nova.model.Hypervisor;
import com.lczyfz.istep.modules.openstack.api.nova.model.Server;
import com.lczyfz.istep.modules.openstack.api.nova.model.portinterface;
import com.lczyfz.istep.modules.openstack.api.nova.model.serveraction.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * ���ڷ�װ�ͽ���Nova API ����ز���JSON 
 * @author jm
 */
public class NovaParse {
	/**
	 * ���� ��������� ���صĽ����json�ַ���
	 * ���� VirtualMachine���getServers����
	 * @param serversJson
	 * @return
	 */
	public static List<Server> resolveServers(JSONObject serversJson){
		List<Server> serverList = new ArrayList<Server>();
		JSONArray servers = serversJson.getJSONArray("servers");
		System.out.println(servers);
		for(int i = 0 ; i < servers.size(); i++){
			Server server = new Server();
			server.setId(servers.getJSONObject(i).getString("id"));
			server.setName(servers.getJSONObject(i).getString("name"));
			//	server.setAccessIPv4(servers.getJSONObject(i).getString("accessIPv4"));
			serverList.add(server);
		}
		return serverList;
	}

	/**
	 * ���� �����������ϸ��Ϣ ���صĽ���json�ַ�
	 * ���� VirtualMachine���getServersDetail����
	 * @param serversJson
	 * @return
	 */
	public static List<Server> resolveServersDetail(JSONObject serversJson){
		List<Server> serverList = new ArrayList<Server>();
		JSONArray servers = serversJson.getJSONArray("servers");
		System.out.println(servers);
		for(int i = 0 ; i < servers.size(); i++){
			Server server = new Server();
			server.setId( servers.getJSONObject(i).getString("id"));
			server.setName( servers.getJSONObject(i).getString("name"));
			server.setStatus( servers.getJSONObject(i).getString("status"));
			serverList.add(server);
		}
		return serverList;
	}

	/**
	 * ���������صĽ���json
	 * ����VirtualMachine���getServerById����
	 * @param serverJson
	 * @return
	 */
	public static Server resolveServerDetail(JSONObject serverJson,String ctfNetworkName){
		JSONObject servers = serverJson.getJSONObject("server");
		Server server = new Server();
		server.setId(servers.getString("id"));
		server.setName(servers.getString("name"));
		server.setStatus(servers.getString("status"));
		System.out.println("dingh=虚拟机详情servers:"+servers);
		if(ctfNetworkName!=null&&!ctfNetworkName.equals(""))
		{
			JSONObject addresses = servers.getJSONObject("addresses");
			if(addresses!=null&&!addresses.equals(""))
			{
				JSONArray addrArray = addresses.getJSONArray(SceneConstants.ctfNetworkName);
				if(addrArray!=null)
				{
					JSONObject addr= addrArray.getJSONObject(0);
					if(addr.getString("addr")!=null)
						server.setIp(addr.getString("addr"));
				}
			}

		}

		return server;
	}

	public static JSONObject packageServer(Server server){

		JSONObject params = new JSONObject();
		JSONObject servers = new JSONObject();
		if(server.getName()!=null)servers.put("name", server.getName());
		if(server.getFlavorRef()!=null)servers.put("flavorRef", server.getFlavorRef());
		if(server.getImageRef()!=null)servers.put("imageRef", server.getImageRef());
		//����Ͷ˿ڶ�ѡһ
		if(server.getNetworks()!=null)servers.put("networks", server.getNetworks());
		if(server.getPorts()!=null)servers.put("networks", server.getPorts());
		if(server.getSecurityGroups()!=null)servers.put("security_groups", server.getSecurityGroups());
		params.put("server", servers);
		System.out.println(params);
		return params;
	}

	/**
	 * �������������ص�json
	 * ����VirtualMachine���addServer����
	 * @param serverJson
	 * @return
	 */
	public static Server resolveCreateServer(JSONObject serverJson){

		JSONObject servers = serverJson.getJSONObject("server");
		Server server = new Server();
		server.setAdminPass(servers.getString("adminPass"));
		server.setId(servers.getString("id"));
		server.setSecurityGroups(servers.getJSONArray("security_groups"));
		return server;
	}

	/**
	 * �������������ص�json
	 * ����VirtualMachine���updateServer����
	 * @param serverJson
	 * @return
	 */
	public static Server resolveUpdateServer(JSONObject serverJson){

		JSONObject servers = serverJson.getJSONObject("server");
		Server server = new Server();
		server.setId(servers.getString("id"));
		server.setName(servers.getString("name"));
		server.setStatus(servers.getString("status"));
		return server;
	}

	/**
	 * ����ȡ�������緵�صĽ���JSON
	 * ����VirtualMachine���getServers����
	 * @param netsJson
	 * @return
	 */
	public static List<Network> resolveNets(JSONObject netsJson){

		List<Network> netList = new ArrayList<Network>();
		JSONArray nets = netsJson.getJSONArray("networks");
		for(int i = 0 ; i < nets.size(); i++){
			Network network = new Network();
			network.setId( nets.getJSONObject(i).getString("id"));

			netList.add(network);
		}
		return netList;
	}

	/**
	 * ����ȡ�������������ͷ��صĽ���JSON
	 * ����FlavorImpl���getFlavor����
	 * @param flavorsJson
	 * @return
	 */
	public static List<Flavor> resolveFlavors(JSONObject flavorsJson){
		List<Flavor> flavorList = new ArrayList<Flavor>();
		JSONArray flavors = flavorsJson.getJSONArray("flavors");
		for(int i = 0 ; i < flavors.size(); i++){
			Flavor flavor = new Flavor();
			flavor.setFlavorId( flavors.getJSONObject(i).getString("id"));
			flavor.setFlavorName( flavors.getJSONObject(i).getString("name"));
			flavorList.add(flavor);
		}
		return flavorList;
	}

	/**
	 * ����ȡ�����������������鷵�صĽ���JSON
	 * ����FlavorImpl���getFlavorDetail����
	 * @param flavorsJson
	 * @return
	 */
	public static List<Flavor> resolveFlavorsDetail(JSONObject flavorsJson){
		List<Flavor> flavorList = new ArrayList<Flavor>();
		JSONArray flavors = flavorsJson.getJSONArray("flavors");
		for(int i = 0 ; i < flavors.size(); i++){
			Flavor flavor = new Flavor();
			flavor.setFlavorId( flavors.getJSONObject(i).getString("id"));
			flavor.setFlavorName( flavors.getJSONObject(i).getString("name"));
			flavor.setFlavorRam( flavors.getJSONObject(i).getString("ram"));
			flavor.setFlavorVcpus( flavors.getJSONObject(i).getString("vcpus"));
			flavor.setFlavorDisk( flavors.getJSONObject(i).getString("disk"));
			flavorList.add(flavor);
		}
		return flavorList;

	}

	/**
	 * ��װ������������������Ҫ���Ĳ���
	 * ����FlavorImpl���createFlavor����
	 * @param flavor
	 * @return
	 */
	public static JSONObject packageFlavor(Flavor flavor){

		JSONObject params = new JSONObject();
		JSONObject flavors = new JSONObject();
		if(flavor.getFlavorName()!=null)flavors.put("name", flavor.getFlavorName());
		if(flavor.getFlavorId()!=null)flavors.put("id", flavor.getFlavorId());
		if(flavor.getFlavorRam()!=null)flavors.put("ram", flavor.getFlavorRam());
		if(flavor.getFlavorDisk()!=null)flavors.put("disk", flavor.getFlavorDisk());
		if(flavor.getFlavorVcpus()!=null)flavors.put("vcpus", flavor.getFlavorVcpus());
		params.put("flavor",flavors);

		return params;

	}

	/**
	 * ����ȡ�����������������鷵�صĽ���JSON
	 * ����FlavorImpl���getFlavorDetail����
	 * @param flavorJson
	 * @return
	 */
	public static Flavor resolveCreateFlavor(JSONObject flavorJson){
		JSONObject flavors = flavorJson.getJSONObject("flavor");
		Flavor flavor = new Flavor();
		flavor.setFlavorId( flavors.getString("id"));
		flavor.setFlavorName( flavors.getString("name"));
		flavor.setFlavorRam( flavors.getString("ram"));
		flavor.setFlavorVcpus( flavors.getString("vcpus"));
		flavor.setFlavorDisk( flavors.getString("disk"));
		return flavor;
	}

	/**
	 * ��װ��ȡ����̨��������Ҫ���Ĳ���
	 * ����ServerActionImpl���GetConsole����
	 * @param consoletype
	 * @return
	 */
	public static JSONObject packageConsoleType(ConsoleType consoletype){

		JSONObject params = new JSONObject();
		JSONObject console = new JSONObject();
		if(consoletype.getType()!=null){
			console.put("type", consoletype.getType());

			if(consoletype.getType()=="rdp-html5")params.put("os-getRDPConsole",console);
			if(consoletype.getType()=="serial")params.put("os-getSerialConsole",console);
			if(consoletype.getType()=="spice-html5")params.put("os-getSPICEConsole",console);
			if(consoletype.getType()=="novnc")params.put("os-getVNCConsole",console);
		}

		return params;

	}


	/**
	 * ����ȡ��ȡ����̨���󷵻صĽ���JSON
	 * ����ServerActionImpl���GetConsole����
	 * @param consoleJson
	 * @return
	 */
	public static ConsoleIndex resolveConsoleType(JSONObject consoleJson){
		JSONObject consoles = consoleJson.getJSONObject("console");
		ConsoleIndex console = new ConsoleIndex();

		console.setType(consoles.getString("type"));
		console.setUrl(consoles.getString("url"));
		//	System.out.println("��ȡ"+consoles.getString("type")+"����̨�ɹ�");


		return console;
	}

	/**
	 * ��װ��ʾ����̨�������Ҫ���Ĳ���
	 * ����ServerActionImpl���ShowConsoleOutput����
	 * @param reqiurelingnum
	 * @return
	 */
	public static JSONObject packageConsoleOutput(ConsoleOutputLineNum reqiurelingnum){

		JSONObject params = new JSONObject();
		JSONObject console = new JSONObject();

		console.put("length", reqiurelingnum.getLength());
		params.put("os-getConsoleOutput", console);

		return params;

	}

	/**
	 * ����ȡ��ʾ����̨������صĽ���JSON
	 * ����ServerActionImpl���ShowConsoleOutput����
	 * @param consoleJson
	 * @return
	 */
	public static ConsoleOutput resolveConsoleOutput(JSONObject consoleJson){

		ConsoleOutput console = new ConsoleOutput();
		console.setOutput(consoleJson.getString("output"));

		return console;
	}

	/**
	 * ��װ��ȫ����ӵ�����������Ҫ���Ĳ���
	 * ����ServerSecurityGroupActionImpl���AddSecurityGroup����
	 * @param securitygroupname
	 * @return
	 */
	public static JSONObject packageAddSecurityGroup(String securitygroupname){

		JSONObject params = new JSONObject();
		JSONObject addsecuritygroup = new JSONObject();

		addsecuritygroup.put("name", securitygroupname);
		params.put("addSecurityGroup", addsecuritygroup);

		return params;

	}

	/**
	 * ��װ��������ɾ��һ����ȫ������Ҫ���Ĳ���
	 * ����ServerSecurityGroupActionImpl���RemoveSecurityGroup����
	 * @param securitygroupname
	 * @return
	 */
	public static JSONObject packageRemoveSecurityGroup(String securitygroupname){

		JSONObject params = new JSONObject();
		JSONObject removesecuritygroup = new JSONObject();

		removesecuritygroup.put("name", securitygroupname);
		params.put("removeSecurityGroup", removesecuritygroup);

		return params;

	}

	/**
	 * ��װ�����������Ҫ���Ĳ���
	 * ����ServerActionsImpl���LockServer����
	 * @return
	 */
	public static JSONObject packageLockServer(){

		JSONObject params = new JSONObject();

		params.put("lock", "null");

		return params;

	}

	/**
	 * ��װ��ͣ����������Ҫ���Ĳ���
	 * ����ServerActionsImpl���PauseServer����
	 * @return
	 */
	public static JSONObject packagePauseServer(){

		JSONObject params = new JSONObject();

		params.put("pause", "null");

		return params;

	}

	/**
	 * ��װ������������������Ҫ���Ĳ���
	 * ����ServerActionsImpl���RebootServer����
	 * @return
	 */
	public static JSONObject packageRebootServer(){

		JSONObject params = new JSONObject();
		JSONObject reboot = new JSONObject();

		reboot.put("type", "HARD");
		params.put("reboot", reboot);

		return params;

	}

	/**
	 * ��װ�ָ��������������Ҫ���Ĳ���
	 * ����ServerActionsImpl���ResumeSuspendedServer����
	 * @return
	 */
	public static JSONObject packageResumeSuspendedServer(){

		JSONObject params = new JSONObject();

		params.put("resume", "null");

		return params;

	}

	/**
	 * ��װ��ԭ�����С�ķ���������Ҫ���Ĳ���
	 * ����ServerActionsImpl���RevertResizedServer����
	 * @return
	 */
	public static JSONObject packageRevertResizedServer(){

		JSONObject params = new JSONObject();

		params.put("revertResize", "null");

		return params;

	}

	/**
	 * ��װ��������������Ҫ���Ĳ���
	 * ����ServerActionsImpl���StartServer����
	 * @return
	 */
	public static JSONObject packageStartServer(){

		JSONObject params = new JSONObject();

		params.put("os-start", "null");

		return params;

	}

	/**
	 * ��װֹͣ����������Ҫ���Ĳ���
	 * ����ServerActionsImpl���StopServer����
	 * @return
	 */
	public static JSONObject packageStopServer(){

		JSONObject params = new JSONObject();

		params.put("os-stop", "null");

		return params;

	}

	/**
	 *  ��װ�������������Ҫ���Ĳ���
	 * ����ServerActionsImpl���SuspendServer����
	 * @return
	 */
	public static JSONObject packageSuspendServer(){

		JSONObject params = new JSONObject();

		params.put("suspend", "null");

		return params;

	}

	/**
	 * ��װ�������������Ҫ���Ĳ���
	 * ����ServerActionsImpl���UnlockServer����
	 * @return
	 */
	public static JSONObject packageUnlockServer(){

		JSONObject params = new JSONObject();

		params.put("unlock", "null");

		return params;

	}

	/**
	 *  ��װȡ����ͣ����������Ҫ���Ĳ���
	 * ����ServerActionsImpl���UnpauseServer����
	 * @return
	 */
	public static JSONObject packageUnpauseServer(){

		JSONObject params = new JSONObject();

		params.put("unpause", "null");

		return params;

	}

	/**
	 * ��װǿ��ɾ�����������Ҫ���Ĳ���
	 * ����ServerActionsImpl���ForceDeleteServer����
	 * @return
	 */
	public static JSONObject packageForceDeleteServer(){

		JSONObject params = new JSONObject();

		params.put("forceDelete", "null");

		return params;

	}

	/**
	 * ��װ�ؽ�����������Ҫ���Ĳ���
	 * ����ServerActionsImpl���RebuildServer����
	 * @param rebuildserver
	 * @return
	 */
	public static JSONObject packageRebuildServer(RebuildServer rebuildserver){

		JSONObject params = new JSONObject();
		JSONObject rebuild = new JSONObject();
		JSONObject metadata = new JSONObject();
		JSONArray personality = new JSONArray();
		JSONObject personaltemp = new JSONObject();
		if(rebuildserver.getImageRef()!=null)rebuild.put("imageRef", rebuildserver.getImageRef());
		if(rebuildserver.getName()!=null)rebuild.put("name", rebuildserver.getName());
		if(rebuildserver.getAdminPass()!=null)rebuild.put("adminPass", rebuildserver.getAdminPass());
		if(rebuildserver.getPreserveEphemeral()!=null)rebuild.put("preserve_ephemeral", rebuildserver.getPreserveEphemeral());
		if(rebuildserver.getMetaVar()!=null){
			metadata.put("meta_var", rebuildserver.getMetaVar());
			rebuild.put("metadata", metadata);
		}
		if(rebuildserver.getPath()!=null)personaltemp.put("path", rebuildserver.getPath());
		if(rebuildserver.getContents()!=null)personaltemp.put("contents", rebuildserver.getContents());
		if(rebuildserver.getPath()!=null||rebuildserver.getContents()!=null){
			personality.add(personaltemp);
			rebuild.put("personality", personality);
		}
		params.put("rebuild", rebuild);

		return params;

	}

	/**
	 * ��װ�����������С����Ҫ���Ĳ���
	 * ����ServerActionsImpl���ResizeServer����
	 * @param flavorRef
	 * @return
	 */
	public static JSONObject packageResizeServer(String flavorRef){

		JSONObject params = new JSONObject();
		JSONObject resize = new JSONObject();
		resize.put("flavorRef", flavorRef);
		resize.put("OS-DCF:diskConfig", "AUTO");
		params.put("resize", resize);

		return params;

	}


	public static List<portinterface> resolvePortInterface(JSONObject PortInterfaceJson){
		List<portinterface> portinterfaceList = new ArrayList<portinterface>();
		JSONArray flavors = PortInterfaceJson.getJSONArray("interfaceAttachments");
		for(int i = 0 ; i < flavors.size(); i++){
			portinterface flavor = new portinterface();
			flavor.setMacAddre( flavors.getJSONObject(i).getString("mac_addr"));
			flavor.setNetId( flavors.getJSONObject(i).getString("net_id"));
			flavor.setPortId( flavors.getJSONObject(i).getString("port_id"));
			flavor.setPortState( flavors.getJSONObject(i).getString("port_state"));
			flavor.setIdAddre( flavors.getJSONObject(i).getJSONArray("fixed_ips").getJSONObject(0).getString("ip_address"));
			flavor.setSubId(flavors.getJSONObject(i).getJSONArray("fixed_ips").getJSONObject(0).getString("subnet_id"));
			portinterfaceList.add(flavor);
		}
		return portinterfaceList;
	}


	public static JSONObject packagePortInterface(portinterface portinter){

		JSONObject params = new JSONObject();
		JSONObject interfaceAttachment = new JSONObject();
		JSONArray fixed_ips = new JSONArray();
		JSONObject ip_address =new JSONObject();

		//if(portinter.getPortId()!=null)interfaceAttachment.put("port_id", portinter.getPortId());
		if(portinter.getIdAddre()!=null)ip_address.put("ip_address", portinter.getIdAddre());
		fixed_ips.add(ip_address);
		interfaceAttachment.put("fixed_ips", fixed_ips);
		if(portinter.getNetId()!=null)interfaceAttachment.put("net_id",portinter.getNetId());
		params.put("interfaceAttachment", interfaceAttachment);

		return params;

	}

	public static portinterface resolvePortInter(JSONObject PortInterfaceJson){
		;
		JSONObject flavors = PortInterfaceJson.getJSONObject("interfaceAttachments");
		portinterface flavor = new portinterface();
		flavor.setMacAddre( flavors.getString("mac_addr"));
		flavor.setNetId( flavors.getString("net_id"));
		flavor.setPortId( flavors.getString("port_id"));
		flavor.setPortState( flavors.getString("port_state"));
		flavor.setIdAddre( flavors.getJSONArray("fixed_ips").getJSONObject(0).getString("ip_address"));
		flavor.setSubId(flavors.getJSONArray("fixed_ips").getJSONObject(0).getString("subnet_id"));


		return flavor;
	}

	public static Hypervisor resolveHypervisor(JSONObject HypervisorJson){
		;
		JSONObject hypervisors = HypervisorJson.getJSONObject("hypervisor_statistics");
		Hypervisor hypervisor = new Hypervisor();
		hypervisor.setCurrentWorkload(hypervisors.getString("current_workload"));
		hypervisor.setDiskAvailableLeast(hypervisors.getString("disk_available_least"));
		hypervisor.setFreeDiskGb(hypervisors.getString("free_disk_gb"));
		hypervisor.setFreeRamMb(hypervisors.getString("free_ram_mb"));
		hypervisor.setLocalGb(hypervisors.getString("local_gb"));
		hypervisor.setLocalGbUsed(hypervisors.getString("local_gb_used"));
		hypervisor.setMemoryMb(hypervisors.getString("memory_mb"));
		hypervisor.setMemoryMbUsed(hypervisors.getString("memory_mb_used"));
		hypervisor.setRunningVms(hypervisors.getString("running_vms"));
		hypervisor.setVcpus(hypervisors.getString("vcpus"));
		hypervisor.setVcpusUsed(hypervisors.getString("vcpus_used"));
		hypervisor.setHypervisorCount(hypervisors.getString("count"));

		return hypervisor;
	}
	
}